<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<div id="page_bar">
    <span style="margin-right: 80px">
        <span>每页显示：</span>
        <input id="pageSize_input" value="${param.pageSize}" name="pageSize"
               style="text-align: center;width: 50px"/>条
        <input id="changePageSize" class="btn btn-primary mb-3" type="button" value="确定">
    </span>
    <script type="text/javascript">
        $(function () {
            $("#changePageSize").click(function () {
                let val = $("#pageSize_input").val();
                if (val < 4) {
                    alert("每页不能少于4条")
                    location.href = "${requestScope.page.pageUrl}&pageSize=4" + "&pageNo=${empty param.pageNo?1:param.pageNo}";
                    return false
                }
                location.href = "${requestScope.page.pageUrl}&pageSize=" + val + "&pageNo=${empty param.pageNo?1:param.pageNo}";
            })
        })
    </script>

    <a class="btn btn-primary mb-3 start" href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=1">首页</a>
    <a class="btn btn-primary mb-3 start"
       href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${requestScope.page.pageNo-1}">上一页</a>
    <c:if test="${requestScope.page.pageNo<=1}">
        <script>
            $(".start").attr("disabled", "disabled")
            console.log("ok")
        </script>

    </c:if>
    <%--输出页码--%>

    <c:choose>
        <%--总页码数小于等于5--%>
        <c:when test="${requestScope.page.pageTotal<=5}">

            <c:forEach begin="1" end="${requestScope.page.pageTotal}" var="i">
                <c:if test="${i==requestScope.page.pageNo}">
                    <a class="btn btn-primary mb-3">${i}</a>
                </c:if>
                <c:if test="${i!=requestScope.page.pageNo}">
                    <a class="btn btn-primary mb-3 not_current"
                       href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${i}">${i}</a>
                </c:if>
            </c:forEach>
        </c:when>
        <%--总页码数大于5--%>
        <c:when test="${requestScope.page.pageTotal>5}">
            <c:choose>
                <%--当前页码为前面3页--%>
                <c:when test="${requestScope.page.pageNo<=3}">
                    <c:forEach begin="1" end="5" var="i">
                        <c:if test="${i==requestScope.page.pageNo}">
                            <a class="btn btn-primary mb-3">${i}</a>
                        </c:if>
                        <c:if test="${i!=requestScope.page.pageNo}">
                            <a class="btn btn-primary mb-3 not_current"
                               href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${i}">${i}</a>
                        </c:if>
                    </c:forEach>
                </c:when>
                <%--当前页码为后面3页--%>
                <c:when test="${requestScope.page.pageNo > requestScope.page.pageTotal-3}">
                    <c:forEach begin="${requestScope.page.pageTotal-4}" end="${requestScope.page.pageTotal}" var="i">
                        <c:if test="${i==requestScope.page.pageNo}">
                            <a class="btn btn-primary mb-3">${i}</a>
                        </c:if>
                        <c:if test="${i!=requestScope.page.pageNo}">
                            <a class="btn btn-primary mb-3 not_current"
                               href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${i}">${i}</a>
                        </c:if>
                    </c:forEach>
                </c:when>
                <c:otherwise>
                    <c:forEach begin="${requestScope.page.pageNo-2}" end="${requestScope.page.pageNo+2}" var="i">
                        <c:if test="${i==requestScope.page.pageNo}">
                            <a class="btn btn-primary mb-3">${i}</a>
                        </c:if>
                        <c:if test="${i!=requestScope.page.pageNo}">
                            <a class="btn btn-primary mb-3 not_current"
                               href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${i}">${i}</a>
                        </c:if>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </c:when>
    </c:choose>

    <a class="btn btn-primary mb-3 end"
       href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${requestScope.page.pageNo+1}">下一页</a>
    <a class="btn btn-primary mb-3 end"
       href="${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=${requestScope.page.pageTotal}">末页</a>
    <c:if test="${requestScope.page.pageNo>=requestScope.page.pageTotal}">
        <script>
            $(".end").attr("disabled", "disabled")
        </script>
    </c:if>

    <span style="font-size: 20px">
        共${requestScope.page.pageTotal}页，${requestScope.page.pageTotalCount}条记录
    到第
    <input value="${empty param.pageNo?1:param.pageNo}" name="pageNo"
             id="pageNo_input" style="text-align: center;width: 50px"/>页
    <input id="searchPageBtn" class="btn btn-primary mb-3" type="button" value="确定">
    </span>



    <script type="text/javascript">
        $(function () {
            $("#searchPageBtn").click(function () {
                let val = $("#pageNo_input").val();
                location.href = "${requestScope.page.pageUrl}&pageSize=${param.pageSize}&pageNo=" + val;
            })
        })
    </script>
</div>
